<?php
function getFormation()
{
	//Récupération des données sur les enseignements
	$req = pg_query('SELECT idformation, nomformation FROM formation');
	
	return $req;
	
}

function getAllOption()
{
	//Récupération des données sur les enseignements
	$req = pg_query("SELECT DISTINCT idOptionOuTc as idoption, nomoption,nbGroupesTd as nbgroupestd,promo FROM optionoutc ");
	
	return $req;
	
}

function getOption($formation)
{
	//Récupération des données sur les enseignements
	$req = pg_query("SELECT DISTINCT idOptionOuTc as idoption, nomoption,nbGroupesTd as nbgroupestd,promo FROM optionoutc WHERE idFormation='$formation'");
	
	return $req;
	
}

function getOptionById($idOption)
{
	//Récupération des données sur les enseignements
	$req = pg_query("SELECT * FROM OptionOuTc WHERE idoptionoutc='$idOption'");
	
	return $req;
	
}


function getEnseignementByEnseignantCM($idenseignant)
{
	$req = pg_query("SELECT DISTINCT e.idenseignement AS id,o.nomoption as option , f.nomformation as formation, e.intitule as nom, e.code as code ,e.datedebut as debut,e.datefin as fin ,e.idue as idue,c.nbhrcm as heurecm,e.idoptionoutc as idoption 
					 FROM enseignement e, optionoutc o , formation f,choixcoursmagistraux c
					 WHERE c.idenseignant=".$idenseignant." 
					 AND c.idEnseignement = e.idEnseignement
					 AND o.idoptionoutc=e.idoptionoutc AND f.idformation=o.idformation ;");
	return $req;
}

function getEnseignementByEnseignantTD($idenseignant)
{
	
	$req = pg_query("SELECT DISTINCT e.idenseignement AS id,o.nomoption as option , f.nomformation as formation, e.intitule as nom, e.code as code ,e.datedebut as debut,e.datefin as fin ,e.idue as idue,c.nbhrpargroupe as heuretd,c.nbgroupetd as groupetd ,e.idoptionoutc as idoption 
					 FROM enseignement e, optionoutc o , formation f,choixtd c
					 WHERE c.idenseignant=".$idenseignant." 
					 AND c.idEnseignement = e.idEnseignement
					 AND o.idoptionoutc=e.idoptionoutc AND f.idformation=o.idformation ;");
	return $req;
}

function getEnseignement($formation,$option)
{
	//Récupération des données sur les enseignements
	$req = pg_query('SELECT DISTINCT e.idenseignement AS id, e.intitule as nom, e.code as code ,e.datedebut as debut,e.datefin as fin FROM enseignement e,formation f,optionoutc o WHERE e.idoptionoutc=o.idoptionoutc AND f.idformation=o.idformation AND f.idformation='.$formation.' AND o.idoptionoutc='.$option);
	
	return $req;
	
}


function getEnseignementById($id)
{
	//Récupération des données sur les enseignements
	$req = pg_query("SELECT DISTINCT e.idenseignement AS id, e.intitule as nom, e.code as code ,e.datedebut as debut,e.datefin as fin ,e.idue as idue,e.nbhrcmtotal as heurecm,e.nbhrtdtotal as heuretd ,e.idoptionoutc as idoption FROM enseignement e WHERE e.idenseignement=$id");
	
	return $req;
	
}

function getCours($idEnseignement,$idoption,$formation)
{
	$req = pg_query("SELECT DISTINCT e.idenseignant AS id,e.nomens as nomens, e.prenomens as prenomens,
	c.datechoix as choix, c.nbhrcm as heure ,en.idenseignement AS idens,o.nomoption as option , 
	f.nomformation as formation, en.intitule as nom, en.code as code ,en.datedebut as debut,en.datefin as fin ,
	en.idue as idue,c.nbhrcm as heurecm,en.idoptionoutc as idoption FROM enseignement en, optionoutc o , 
	formation f,choixcoursmagistraux c,enseignant e 
	WHERE c.idenseignant=e.idenseignant AND c.idEnseignement=en.idEnseignement AND
	 c.idEnseignement=$idEnseignement AND en.idoptionoutc = o.idoptionoutc AND en.idoptionoutc=$idoption 
	 AND o.idformation = f.idformation AND f.idformation=$formation;");

	return $req;
	
}

function getTD($idEnseignement,$idoption,$formation)
{
	$req = pg_query("SELECT DISTINCT e.idenseignant AS id,e.nomens as nomens, e.prenomens as prenomens,
	c.datechoix as choix, c.nbhrpargroupe as heure,c.nbgroupetd as groupetd  ,en.idenseignement AS id,
	o.nomoption as option , f.nomformation as formation, en.intitule as nom, en.code as code ,
	en.datedebut as debut,en.datefin as fin ,en.idue as idue,en.idoptionoutc as idoption 
					 FROM enseignement en, optionoutc o , formation f,choixtd c,enseignant e
					 WHERE c.idenseignant=e.idenseignant 
					 AND c.idEnseignement = en.idEnseignement
					 AND en.idEnseignement=$idEnseignement
					 AND en.idoptionoutc=$idoption
					 AND o.idoptionoutc = en.idoptionoutc
					 AND o.idoptionoutc=$idoption 
					 AND o.idformation=f.idformation
					 AND f.idformation=$formation ;");
	return $req;
	
}

function getAllUe()
{
	//Récupération des données sur les ue
	$req = pg_query('SELECT DISTINCT idue,nomue FROM ue');
	

	return $req;
}

function ajoutOption($nomoption,$nbgroupestd,$promo,$idformation){
	pg_query("INSERT INTO OptionOuTc (nomoption,nbgroupestd,promo,idformation) VALUES ('$nomoption','$nbgroupestd','$promo','$idformation')");
}

function updateOption($id,$nomoption,$nbgroupestd,$promo,$idformation){
	pg_query("UPDATE OptionOuTc SET nomoption='$nomoption',nbgroupestd='$nbgroupestd',promo='$promo',idformation='$idformation' WHERE idoptionoutc=$id");
}

function ajoutEnseignement($intitule, $code, $nbhrcmtotal, $nbhrtdtotal, $datedebut, $datefin, $idoptionoutc, $idue)
{
	//Récupération des données sur les enseignements
	pg_query("INSERT INTO enseignement (intitule, code, nbhrcmtotal, nbhrtdtotal, datedebut, datefin, idoptionoutc, idue) VALUES ('$intitule','$code','$nbhrcmtotal','$nbhrtdtotal','$datedebut','$datefin','$idoptionoutc','$idue')");
}

function suppressionFormation($id){
	 $req = getOption($id);
	 while($row = pg_fetch_assoc($req)){
	 	    $idOption = $row['idoption'];
	 	    pg_query("DELETE FROM enseignement WHERE idoptionoutc=$id");
	 }
	 pg_query("DELETE FROM optionoutc WHERE idformation=$id");
	 pg_query("DELETE FROM formation WHERE idformation=$id");
}

function suppressionEnseignement($id){
	   pg_query("DELETE FROM choixcoursmagistraux WHERE idenseignement=$id");
	  pg_query("DELETE FROM choixtd WHERE idenseignement=$id");
	 pg_query("DELETE FROM enseignement WHERE idenseignement=$id");
}

function suppressionOption($idoption,$idformation){
	  $req =  getEnseignement($idformation,$idoption);
	  while($row = pg_fetch_assoc($req)){
	  	     $id = $row['id'];
		     //echo "DELETE FROM choixcoursmagistraux WHERE idenseignement=$id";
	  	     suppressionEnseignement($id);
	  }
	  pg_query("DELETE FROM optionoutc WHERE idoptionoutc=$idoption");
}

function updateEnseignement($idenseignement,$intitule, $code, $nbhrcmtotal, $nbhrtdtotal, $datedebut, $datefin, $idoptionoutc, $idue)
{
	//Récupération des données sur les enseignements
	pg_query("UPDATE enseignement  SET intitule ='$intitule',code ='$code',nbhrcmtotal ='$nbhrcmtotal',nbhrtdtotal ='$nbhrtdtotal',datedebut ='$datedebut',datefin ='$datefin',idoptionoutc ='$idoptionoutc',idue ='$idue' WHERE idenseignement = $idenseignement");
}
?>
